<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.3.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"example.com","root":"/","scheme":"Muse","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.json"};
  </script>

  <meta name="description" content="搬砖 嵌入式">
<meta property="og:type" content="website">
<meta property="og:title" content="学无止境">
<meta property="og:url" content="http://example.com/page/21/index.html">
<meta property="og:site_name" content="学无止境">
<meta property="og:description" content="搬砖 嵌入式">
<meta property="og:locale" content="zh_CN">
<meta property="article:author" content="xudonglei">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://example.com/page/21/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : true,
    isPost : false,
    lang   : 'zh-CN'
  };
</script>

  <title>学无止境</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">学无止境</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">Do not bb, show me the example</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-fw fa-th"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content index posts-expand">
            
      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/09/13/%E8%BF%90%E5%8A%A8%E5%B0%8F%E8%BD%A6%E4%B8%AD%E7%9A%84%E5%8D%A1%E5%B0%94%E6%9B%BC%E6%BB%A4%E6%B3%A2/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="xudonglei">
      <meta itemprop="description" content="搬砖 嵌入式">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="学无止境">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/2021/09/13/%E8%BF%90%E5%8A%A8%E5%B0%8F%E8%BD%A6%E4%B8%AD%E7%9A%84%E5%8D%A1%E5%B0%94%E6%9B%BC%E6%BB%A4%E6%B3%A2/" class="post-title-link" itemprop="url">运动小车中的卡尔曼滤波</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-09-13 09:12:11" itemprop="dateCreated datePublished" datetime="2021-09-13T09:12:11+08:00">2021-09-13</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E7%AE%97%E6%B3%95/" itemprop="url" rel="index"><span itemprop="name">算法</span></a>
                </span>
            </span>

          
            <span id="/2021/09/13/%E8%BF%90%E5%8A%A8%E5%B0%8F%E8%BD%A6%E4%B8%AD%E7%9A%84%E5%8D%A1%E5%B0%94%E6%9B%BC%E6%BB%A4%E6%B3%A2/" class="post-meta-item leancloud_visitors" data-flag-title="运动小车中的卡尔曼滤波" title="阅读次数">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span class="leancloud-visitors-count"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="fa fa-comment-o"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2021/09/13/%E8%BF%90%E5%8A%A8%E5%B0%8F%E8%BD%A6%E4%B8%AD%E7%9A%84%E5%8D%A1%E5%B0%94%E6%9B%BC%E6%BB%A4%E6%B3%A2/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2021/09/13/%E8%BF%90%E5%8A%A8%E5%B0%8F%E8%BD%A6%E4%B8%AD%E7%9A%84%E5%8D%A1%E5%B0%94%E6%9B%BC%E6%BB%A4%E6%B3%A2/" itemprop="commentCount"></span>
    </a>
  </span>
  
  

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <p>​        以运动的小车为例，简单介绍卡尔曼滤波的五个公式的推导。推导过程使用了B站Up主<a target="_blank" rel="noopener" href="https://space.bilibili.com/352976834%E5%85%B3%E4%BA%8E%E5%8D%A1%E5%B0%94%E6%9B%BC%E4%BB%8B%E7%BB%8D%E7%9A%84%E8%A7%86%E9%A2%91%EF%BC%8C%E8%AE%B2%E7%9A%84%E7%94%9A%E8%87%B3%E6%AF%94%E5%9B%BD%E5%A4%96%E7%9A%84%E4%B8%80%E4%BA%9B%E8%A7%86%E9%A2%91%E6%95%99%E7%A8%8B%E6%9B%B4%E5%8A%A0%E9%80%9A%E4%BF%97%E6%98%93%E6%87%82%EF%BC%8C%E9%9D%9E%E5%B8%B8%E6%84%9F%E8%B0%A2%E4%BB%96%E3%80%82">https://space.bilibili.com/352976834关于卡尔曼介绍的视频，讲的甚至比国外的一些视频教程更加通俗易懂，非常感谢他。</a></p>
<h3 id="1-预测部分推导"><a href="#1-预测部分推导" class="headerlink" title="1.预测部分推导"></a>1.预测部分推导</h3><img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/微信图片_20210911185400.jpg" style="zoom:37%;">

<h3 id="2-更新部分"><a href="#2-更新部分" class="headerlink" title="2.更新部分"></a>2.更新部分</h3><img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/微信截图_20210911213308.png" style="zoom:65%;">

<p><strong>其中：Q和R是卡尔曼滤波器中主要需要调的东西。Q是过程噪声的方差，R是观测噪声的方差。</strong></p>
<h3 id="3-简单总结"><a href="#3-简单总结" class="headerlink" title="3.简单总结"></a>3.简单总结</h3><img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/微信截图_20210912093920.png" style="zoom:67%;">

<h3 id="4-具体python代码例子"><a href="#4-具体python代码例子" class="headerlink" title="4.具体python代码例子"></a>4.具体python代码例子</h3><p>​        这边以匀速运动的小车为例，介绍python中filterpy.kalman的使用。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">输入参数:</span><br><span class="line">x : ndarray (dim_x, <span class="number">1</span>), default = [<span class="number">0</span>,<span class="number">0</span>,<span class="number">0</span>…<span class="number">0</span>] 表示滤波器需要估计的状态向量</span><br><span class="line">P : ndarray (dim_x, dim_x), default eye(dim_x) 表示协方差矩阵</span><br><span class="line">Q : ndarray (dim_x, dim_x), default eye(dim_x) 表示过程噪声（系统噪声）</span><br><span class="line">R : ndarray (dim_z, dim_z), default eye(dim_x) 表示量测噪声</span><br><span class="line">H : ndarray (dim_z, dim_x) 表示量测方程</span><br><span class="line">F : ndarray (dim_x, dim_x) 表示状态转移方程</span><br><span class="line">B : ndarray (dim_x, dim_u), default <span class="number">0</span> 表示控制转移矩阵</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> matplotlib <span class="keyword">import</span> pyplot <span class="keyword">as</span> plt</span><br><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line"><span class="keyword">from</span> filterpy.kalman <span class="keyword">import</span> KalmanFilter</span><br><span class="line"></span><br><span class="line"><span class="comment">######小车运动数据生成</span></span><br><span class="line"><span class="comment"># 生成100个位置，从1到100，是小车的实际位置</span></span><br><span class="line">z = np.linspace(<span class="number">1</span>,<span class="number">100</span>,<span class="number">100</span>)</span><br><span class="line"><span class="comment"># 添加噪声</span></span><br><span class="line">mu, sigma = <span class="number">0</span>, <span class="number">1</span></span><br><span class="line">noise = np.random.normal(mu, sigma, <span class="number">100</span>)</span><br><span class="line"><span class="comment"># 小车位置的观测值</span></span><br><span class="line">z_nosie = z+noise</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">##############参数初始化</span></span><br><span class="line"><span class="comment"># dim_x 状态向量size,在该例中为[p,v]，即位置和速度,size=2</span></span><br><span class="line"><span class="comment"># dim_z 测量向量size，假设小车为匀速，速度为1，测量向量只观测位置，size=1</span></span><br><span class="line">my_filter = KalmanFilter(dim_x=<span class="number">2</span>, dim_z=<span class="number">1</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 定义卡尔曼滤波中所需的参数</span></span><br><span class="line"><span class="comment"># x 初始状态为[0,0],即初始位置为0，速度为0.</span></span><br><span class="line"><span class="comment"># 这个初始值不是非常重要，在利用观测值进行更新迭代后会接近于真实值</span></span><br><span class="line">my_filter.x = np.array([<span class="number">0</span>, <span class="number">0</span>]).reshape(<span class="number">2</span>, <span class="number">1</span>)  <span class="comment"># 注意这里要reshape</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># p 协方差矩阵，表示状态向量内位置与速度的相关性</span></span><br><span class="line"><span class="comment"># 假设速度与位置没关系，协方差矩阵为[[1,0],[0,1]]</span></span><br><span class="line">my_filter.P = np.array([[<span class="number">1</span>, <span class="number">0</span>], [<span class="number">0</span>, <span class="number">1</span>]])</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># F 初始的状态转移矩阵，假设为匀速运动模型，可将其设为如下所示</span></span><br><span class="line">dt = <span class="number">1</span></span><br><span class="line">my_filter.F = np.array([[<span class="number">1</span>, dt], [<span class="number">0</span>, <span class="number">1</span>]])  <span class="comment"># shape为：(2，2)</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># Q 状态转移协方差矩阵，也就是外界噪声，因为它的形状默认是(dim_x, dim_x)</span></span><br><span class="line"><span class="comment"># 在该例中假设小车匀速，外界干扰小，所以我们对F非常确定，觉得F一定不会出错，所以Q设的很小</span></span><br><span class="line">my_filter.Q = np.array([[<span class="number">0.0001</span>, <span class="number">0</span>], [<span class="number">0.</span>, <span class="number">0.0001</span>]])</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 观测矩阵 Hx = p</span></span><br><span class="line"><span class="comment"># 利用观测数据对预测进行更新，观测矩阵的左边一项不能设置成0</span></span><br><span class="line">my_filter.H = np.array([[<span class="number">1</span>, <span class="number">0</span>]])</span><br><span class="line"><span class="comment"># R 测量噪声，方差为1</span></span><br><span class="line">my_filter.R = <span class="number">1</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 保存卡尔曼滤波过程中的位置和速度</span></span><br><span class="line">z_new_list = []</span><br><span class="line">v_new_list = []</span><br><span class="line"><span class="comment"># 对于每一个观测值，进行一次卡尔曼滤波</span></span><br><span class="line"><span class="keyword">for</span> k <span class="keyword">in</span> <span class="built_in">range</span>(<span class="built_in">len</span>(z_nosie)):</span><br><span class="line">    <span class="comment"># 预测过程</span></span><br><span class="line">    my_filter.predict()</span><br><span class="line">    <span class="comment"># 利用观测值进行更新</span></span><br><span class="line">    my_filter.update(z_nosie[k])</span><br><span class="line">    <span class="comment"># do something with the output</span></span><br><span class="line">    x = my_filter.x</span><br><span class="line">    <span class="comment"># 收集卡尔曼滤波后的速度和位置信息</span></span><br><span class="line">    z_new_list.append(x[<span class="number">0</span>][<span class="number">0</span>])</span><br><span class="line">    v_new_list.append(x[<span class="number">1</span>][<span class="number">0</span>])</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">plt.subplot(<span class="number">111</span>)</span><br><span class="line">plt.plot(z_new_list, <span class="string">&#x27;r&#x27;</span>, linewidth=<span class="number">1</span>)</span><br><span class="line">plt.plot(z, <span class="string">&#x27;b&#x27;</span>, linewidth=<span class="number">1</span>)</span><br><span class="line">plt.plot(z_nosie, <span class="string">&#x27;g&#x27;</span>, linewidth=<span class="number">1</span>)</span><br><span class="line">plt.show()</span><br></pre></td></tr></table></figure>
<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/微信截图_20210912203447.png" style="zoom:39%;">

<p><img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/%E5%BE%AE%E4%BF%A1%E6%88%AA%E5%9B%BE_20210912204944.png"></p>
<p>​        卡尔曼滤波后的红色那线和观测值相差比较远，因为测量噪声my_filter.R比外界噪声my_filter.Q大很多，肯定不会信任测量值。</p>

      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/09/13/%E4%BB%8E%E5%B7%A5%E7%A8%8B%E8%A7%92%E5%BA%A6%E5%AD%A6%E4%B9%A0%E5%8D%A1%E5%B0%94%E6%9B%BC%E6%BB%A4%E6%B3%A2/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="xudonglei">
      <meta itemprop="description" content="搬砖 嵌入式">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="学无止境">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/2021/09/13/%E4%BB%8E%E5%B7%A5%E7%A8%8B%E8%A7%92%E5%BA%A6%E5%AD%A6%E4%B9%A0%E5%8D%A1%E5%B0%94%E6%9B%BC%E6%BB%A4%E6%B3%A2/" class="post-title-link" itemprop="url">从工程角度学习卡尔曼滤波</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-09-13 09:12:10" itemprop="dateCreated datePublished" datetime="2021-09-13T09:12:10+08:00">2021-09-13</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E7%AE%97%E6%B3%95/" itemprop="url" rel="index"><span itemprop="name">算法</span></a>
                </span>
            </span>

          
            <span id="/2021/09/13/%E4%BB%8E%E5%B7%A5%E7%A8%8B%E8%A7%92%E5%BA%A6%E5%AD%A6%E4%B9%A0%E5%8D%A1%E5%B0%94%E6%9B%BC%E6%BB%A4%E6%B3%A2/" class="post-meta-item leancloud_visitors" data-flag-title="从工程角度学习卡尔曼滤波" title="阅读次数">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span class="leancloud-visitors-count"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="fa fa-comment-o"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2021/09/13/%E4%BB%8E%E5%B7%A5%E7%A8%8B%E8%A7%92%E5%BA%A6%E5%AD%A6%E4%B9%A0%E5%8D%A1%E5%B0%94%E6%9B%BC%E6%BB%A4%E6%B3%A2/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2021/09/13/%E4%BB%8E%E5%B7%A5%E7%A8%8B%E8%A7%92%E5%BA%A6%E5%AD%A6%E4%B9%A0%E5%8D%A1%E5%B0%94%E6%9B%BC%E6%BB%A4%E6%B3%A2/" itemprop="commentCount"></span>
    </a>
  </span>
  
  

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <p>​        第一次接触卡尔曼滤波的时候，被它的五个核心公式给搞晕了，推导的过程也还蛮复杂的，可能当时推导出来了，过了一段时间又忘记了。这边就从工程人运用的角度去简单入门卡尔曼滤波，不去考虑其中复杂的数学公式。</p>
<p>​        卡尔曼有五个核心的公式，这些公式里面有一些符号和基本概念，我们需要提前了解，下面就简单介绍，其中引用了B站up主<a target="_blank" rel="noopener" href="https://space.bilibili.com/352976834%E8%A7%86%E9%A2%91%E8%AE%B2%E8%A7%A3%E4%B8%AD%E7%9A%84%E5%9B%BE%EF%BC%8C%E5%9C%A8%E8%BF%99%E9%87%8C%E5%AF%B9%E4%BB%96%E8%A1%A8%E7%A4%BA%E7%94%B1%E8%A1%B7%E7%9A%84%E6%84%9F%E8%B0%A2%E3%80%82">https://space.bilibili.com/352976834视频讲解中的图，在这里对他表示由衷的感谢。</a></p>
<h3 id="1-基本的滤波知识"><a href="#1-基本的滤波知识" class="headerlink" title="1.基本的滤波知识"></a>1.基本的滤波知识</h3><img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/微信截图_20210911142442.png" style="zoom:52%;">

<h3 id="2-卡尔曼直观图解"><a href="#2-卡尔曼直观图解" class="headerlink" title="2.卡尔曼直观图解"></a>2.卡尔曼直观图解</h3><p><img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/微信截图_20210911143323.png" style="zoom: 67%;"><img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/%E5%BE%AE%E4%BF%A1%E6%88%AA%E5%9B%BE_20210911145112.png"></p>
<p>​        图中纵坐标是概率密度，^代表估计值，不带^的是测量值。有横杠的代表先验估计值；没有横杠的是最优估计也叫做修正值，后验估计值。</p>
<p>​        最左边那个是上一时刻的最优估计值，就是卡尔曼滤波最终输出的值。中间那个是基于最左边那个估计出来的一个当前的估计值。最右边那个是当前时刻的观测值。那么当前时刻的最优估计值大概率是在下图中新画出来的地方，总结一下意思就是：当前的最优估计值是由先验估计和当前的观测值取公有的部分得到一个最优的估计值。</p>
<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/微信截图_20210911145534.png" style="zoom:67%;">

<h3 id="3-卡尔曼公式简单理解"><a href="#3-卡尔曼公式简单理解" class="headerlink" title="3.卡尔曼公式简单理解"></a>3.卡尔曼公式简单理解</h3><p>​        实现过程：使用上一次的最优结果预测当前的值，(<strong>这个值叫做先验估计</strong>)</p>
<p>​                            同时使用观测值修正当前值，得到最优结果。</p>
<p><img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/%E5%BE%AE%E4%BF%A1%E6%88%AA%E5%9B%BE_20210911152409.png"></p>
<p>公式1：基于之前的最优估计来推出当前的先验估计。</p>
<p>公式2：是协方差公式，对应公式1先验估计的协方差。(先验估计值和真实值之间误差的协方差矩阵)</p>
<p>公式3：计算卡尔曼增益。</p>
<p>公式4：最优估计。加号左边那个是先验估计值，加号右边那个是观测值，Kt是一个权重，然后两者融合。</p>
<p>公式5：计算最优估计值和真实值之间误差的协方差矩阵。</p>
<p><strong>其中：Q和R是卡尔曼滤波器中主要需要调的东西。Q是过程噪声的方差，R是观测噪声的方差。</strong></p>
<h3 id="4-简单的matlab例子"><a href="#4-简单的matlab例子" class="headerlink" title="4. 简单的matlab例子"></a>4. 简单的matlab例子</h3><figure class="highlight matlab"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line">close all; </span><br><span class="line">clear all;</span><br><span class="line"><span class="comment">%系统方程：X(k+1)=A*X(k)+w(k) </span></span><br><span class="line"><span class="comment">%观测方程：Z(k)=H*X(k)+v(k) </span></span><br><span class="line">A=<span class="number">1</span>; </span><br><span class="line">H=<span class="number">1</span>; </span><br><span class="line">X(<span class="number">1</span>)=<span class="number">25</span>; <span class="comment">%系统状态初始化 </span></span><br><span class="line"></span><br><span class="line">w=<span class="number">1</span>*<span class="built_in">randn</span>(<span class="number">100</span>,<span class="number">1</span>); <span class="comment">%系统噪声 </span></span><br><span class="line">v=<span class="number">0.1</span>*<span class="built_in">randn</span>(<span class="number">100</span>,<span class="number">1</span>); <span class="comment">%测量噪声 </span></span><br><span class="line">Q=cov(w); </span><br><span class="line">R=cov(v); </span><br><span class="line"></span><br><span class="line">Xe(<span class="number">1</span>)=<span class="number">25</span>; <span class="comment">%状态估计初值 </span></span><br><span class="line">Pe(<span class="number">1</span>)=<span class="number">0</span>; <span class="comment">%估计值与真实值之间的协方差矩阵初值 </span></span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> <span class="built_in">i</span>=<span class="number">2</span>:<span class="number">100</span> </span><br><span class="line">	X(<span class="built_in">i</span>)=A*X(<span class="built_in">i</span><span class="number">-1</span>)+w(<span class="built_in">i</span>); <span class="comment">% 【6】系统方程 </span></span><br><span class="line">	Z(<span class="built_in">i</span>)=H*X(<span class="built_in">i</span>)+v(<span class="built_in">i</span>);   <span class="comment">% 【7】观测方程 </span></span><br><span class="line">	</span><br><span class="line">	<span class="comment">%卡尔曼滤波五大核心方程 </span></span><br><span class="line">	Xp(<span class="built_in">i</span>)=A*Xe(<span class="built_in">i</span><span class="number">-1</span>);      <span class="comment">% 【1】根据滤波后最优估计值计算预测值 </span></span><br><span class="line">	Pp(<span class="built_in">i</span>)=A*Pe(<span class="built_in">i</span><span class="number">-1</span>)*A&#x27;+Q; <span class="comment">% 【2】计算预测值与真实值之间误差的协方差矩阵</span></span><br><span class="line">    K(<span class="built_in">i</span>)=Pp(<span class="built_in">i</span>)*H&#x27;/((H*Pp(<span class="built_in">i</span>)*H&#x27;+R));  <span class="comment">% 【3】计算卡尔曼增益 </span></span><br><span class="line">    Xe(<span class="built_in">i</span>)=Xp(<span class="built_in">i</span>)+K(<span class="built_in">i</span>)*(Z(<span class="built_in">i</span>)-H*Xp(<span class="built_in">i</span>)); <span class="comment">% 【4】计算滤波后估计值 </span></span><br><span class="line">    Pe(<span class="built_in">i</span>)=Pp(<span class="built_in">i</span>)-K(<span class="built_in">i</span>)*H*Pp(<span class="built_in">i</span>);        <span class="comment">% 【5】计算估计值与真实值之间误差的协方差矩阵 </span></span><br><span class="line"><span class="keyword">end</span> </span><br><span class="line"></span><br><span class="line">n=<span class="number">1</span>:<span class="number">100</span>; </span><br><span class="line">subplot(<span class="number">211</span>) </span><br><span class="line"><span class="built_in">plot</span>(n,Xp,<span class="string">&#x27;r&#x27;</span>,<span class="string">&#x27;linewidth&#x27;</span>,<span class="number">2</span>); <span class="comment">%绘制预测值 </span></span><br><span class="line"><span class="built_in">hold</span> on; </span><br><span class="line"><span class="built_in">plot</span>(n,Z,<span class="string">&#x27;b&#x27;</span>,<span class="string">&#x27;linewidth&#x27;</span>,<span class="number">2</span>); <span class="comment">%绘制测量值 </span></span><br><span class="line"><span class="built_in">hold</span> on; </span><br><span class="line"><span class="built_in">plot</span>(n,Xe,<span class="string">&#x27;g&#x27;</span>,<span class="string">&#x27;linewidth&#x27;</span>,<span class="number">2</span>); <span class="comment">%绘制估计值 </span></span><br><span class="line"><span class="built_in">hold</span> on; </span><br><span class="line"><span class="built_in">plot</span>(n,X-Xe,<span class="string">&#x27;k&#x27;</span>,<span class="string">&#x27;linewidth&#x27;</span>,<span class="number">2</span>); <span class="comment">%绘制误差 </span></span><br><span class="line"><span class="built_in">legend</span>(<span class="string">&#x27;预测值&#x27;</span>,<span class="string">&#x27;观测值&#x27;</span>,<span class="string">&#x27;滤波值&#x27;</span>,<span class="string">&#x27;误差&#x27;</span>) </span><br><span class="line">grid </span><br><span class="line">subplot(<span class="number">212</span>) </span><br><span class="line"><span class="built_in">plot</span>(n,K,<span class="string">&#x27;b&#x27;</span>,<span class="string">&#x27;linewidth&#x27;</span>,<span class="number">2</span>); <span class="comment">%绘制卡尔曼增益 </span></span><br><span class="line"><span class="built_in">legend</span>(<span class="string">&#x27;卡尔曼增益&#x27;</span>) </span><br><span class="line">grid</span><br></pre></td></tr></table></figure>
<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/微信截图_20210911160357.png" style="zoom:67%;">

<p>​    仿真结果如上上图所示，不难发现，由于测量噪声相比于系统噪声小的多，所以估计值更倾向于观测值，此时的卡尔曼增益趋近于 1。</p>

      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/09/02/linux%E5%86%85%E6%A0%B8%E4%B8%AD%E7%9A%84kfifo/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="xudonglei">
      <meta itemprop="description" content="搬砖 嵌入式">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="学无止境">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/2021/09/02/linux%E5%86%85%E6%A0%B8%E4%B8%AD%E7%9A%84kfifo/" class="post-title-link" itemprop="url">linux内核中的kfifo</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-09-02 13:45:15" itemprop="dateCreated datePublished" datetime="2021-09-02T13:45:15+08:00">2021-09-02</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Linux/" itemprop="url" rel="index"><span itemprop="name">Linux</span></a>
                </span>
            </span>

          
            <span id="/2021/09/02/linux%E5%86%85%E6%A0%B8%E4%B8%AD%E7%9A%84kfifo/" class="post-meta-item leancloud_visitors" data-flag-title="linux内核中的kfifo" title="阅读次数">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span class="leancloud-visitors-count"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="fa fa-comment-o"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2021/09/02/linux%E5%86%85%E6%A0%B8%E4%B8%AD%E7%9A%84kfifo/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2021/09/02/linux%E5%86%85%E6%A0%B8%E4%B8%AD%E7%9A%84kfifo/" itemprop="commentCount"></span>
    </a>
  </span>
  
  

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <p>​    Linux内核里面有很多简洁、优雅、高效的代码，非常有技巧性，比如说内核中的kfifo，它采用的是环形缓冲区实现，好处在于：当一个数据元素被用掉后，其余数据元素不需要移动其存储位置，从而减少拷贝提高效率。另外它使用并行无锁编程技术，即当它用于只有一个入队线程和一个出队线程的场景时，两个线程可以并发操作，而不需要任何加锁行为，就可以保证kfifo的线程安全。</p>
<p>​    参考的文档如下所示：</p>
<p><a target="_blank" rel="noopener" href="https://blog.csdn.net/linyt/article/details/53355355">https://blog.csdn.net/linyt/article/details/53355355</a></p>
<p><a target="_blank" rel="noopener" href="https://zhuanlan.zhihu.com/p/357380466">https://zhuanlan.zhihu.com/p/357380466</a></p>
<p><a target="_blank" rel="noopener" href="https://blog.csdn.net/weixin_44062361/article/details/108749870?utm_medium=distribute.pc_relevant.none-task-blog-2~default~BlogCommendFromBaidu~default-18.essearch_pc_relevant&amp;depth_1-utm_source=distribute.pc_relevant.none-task-blog-2~default~BlogCommendFromBaidu~default-18.essearch_pc_relevant">https://blog.csdn.net/weixin_44062361/article/details/108749870?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-18.essearch_pc_relevant&amp;depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-18.essearch_pc_relevant</a></p>
<p><a target="_blank" rel="noopener" href="https://blog.csdn.net/BerylLe_/article/details/106669761?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_baidulandingword~default-4.essearch_pc_relevant&amp;spm=1001.2101.3001.4242">https://blog.csdn.net/BerylLe_/article/details/106669761?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_baidulandingword~default-4.essearch_pc_relevant&amp;spm=1001.2101.3001.4242</a></p>
<p>​    下面先直接给出简化后的代码：</p>
<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/微信截图_20210901165856.png" style="zoom:67%;">

<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/微信截图_20210901170051.png" style="zoom:67%;">

<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/微信截图_20210901170144.png" style="zoom:67%;">

<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/微信图片_20210901170830.jpg" style="zoom:30%;">

<p>​    其中Write_RingBuff这个函数对着上面这个手写的图一行行分析代码基本就可以看懂。</p>
<p>​    <strong>注意：</strong>kfifo-&gt;in % kfifo-&gt;size 可以转化为 **kfifo-&gt;in &amp; (kfifo-&gt;size – 1)**，前提是kfifo-&gt;size是2的幂次方。</p>
<p>​                122行rb-&gt;size - (rb-&gt;out &amp; (rb-&gt;size - 1))为：rb-&gt;out 到 buffer末尾所剩余的长度，l取len和剩余长度的最小值。</p>
<p><strong>骚操作：</strong></p>
<p>​    kfifo里面还有一个特别骚的操作，看完不得不直呼牛逼。104行，注意这里 只是用了 rb-&gt;in += len而未取模，这就是kfifo的设计精妙之处，这里用到了unsigned int的溢出性质，当in 持续增加到溢出时又会被置为0，这样就节省了每次in向前增加都要取模的性能，精益求精，真的是牛逼。</p>
<p>​    下面这个图解是<a target="_blank" rel="noopener" href="https://blog.csdn.net/weixin_44062361/article/details/108749870?utm_medium=distribute.pc_relevant.none-task-blog-2~default~BlogCommendFromBaidu~default-18.essearch_pc_relevant&amp;depth_1-utm_source=distribute.pc_relevant.none-task-blog-2~default~BlogCommendFromBaidu~default-18.essearch_pc_relevant%E4%B8%AD%E7%9A%84%E7%BD%91%E5%8F%8B%E5%81%9A%E7%9A%84%EF%BC%8C%E8%BF%99%E9%87%8C%E5%BC%95%E7%94%A8%E4%B8%80%E4%B8%8B%E5%8A%A0%E6%B7%B1%E7%90%86%E8%A7%A3%E3%80%82">https://blog.csdn.net/weixin_44062361/article/details/108749870?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-18.essearch_pc_relevant&amp;depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-18.essearch_pc_relevant中的网友做的，这里引用一下加深理解。</a></p>
<p>​    图解一下 in 先溢出的情况，size = 64， 写入前 in = 4294967291， out = 4294967279 ，数据 in – out = 12;</p>
<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/微信截图_20210901212015.png" style="zoom:47%;">

<p>​    写入 数据16个字节，则 in + 16 = 4294967307，溢出为 11，此时 in – out = –4294967268，溢出为28，数据长度仍然正确，由此可见，在这种特殊情况下，这种计算仍然正确，真的是牛逼。</p>
<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/微信截图_20210901213034.png" style="zoom:47%;">



<p><strong>补充：</strong></p>
<p>​    读取速度需要比写入的速度快，否则缓冲区满了会使数据丢失。</p>

      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/08/25/windows%E4%B8%8A%E4%BD%BF%E7%94%A8VSCode%20CMake%E6%9E%84%E5%BB%BAC%20C++%20IDE/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="xudonglei">
      <meta itemprop="description" content="搬砖 嵌入式">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="学无止境">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/2021/08/25/windows%E4%B8%8A%E4%BD%BF%E7%94%A8VSCode%20CMake%E6%9E%84%E5%BB%BAC%20C++%20IDE/" class="post-title-link" itemprop="url">windows上使用VSCode CMake构建C C++ IDE</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-08-25 09:12:41" itemprop="dateCreated datePublished" datetime="2021-08-25T09:12:41+08:00">2021-08-25</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E5%B7%A5%E5%85%B7/" itemprop="url" rel="index"><span itemprop="name">工具</span></a>
                </span>
            </span>

          
            <span id="/2021/08/25/windows%E4%B8%8A%E4%BD%BF%E7%94%A8VSCode%20CMake%E6%9E%84%E5%BB%BAC%20C++%20IDE/" class="post-meta-item leancloud_visitors" data-flag-title="windows上使用VSCode CMake构建C C++ IDE" title="阅读次数">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span class="leancloud-visitors-count"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="fa fa-comment-o"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2021/08/25/windows%E4%B8%8A%E4%BD%BF%E7%94%A8VSCode%20CMake%E6%9E%84%E5%BB%BAC%20C++%20IDE/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2021/08/25/windows%E4%B8%8A%E4%BD%BF%E7%94%A8VSCode%20CMake%E6%9E%84%E5%BB%BAC%20C++%20IDE/" itemprop="commentCount"></span>
    </a>
  </span>
  
  

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <h2 id="一、准备需要的软件"><a href="#一、准备需要的软件" class="headerlink" title="一、准备需要的软件"></a>一、准备需要的软件</h2><h3 id="1-1-cmake"><a href="#1-1-cmake" class="headerlink" title="1.1 cmake"></a>1.1 cmake</h3><p><a target="_blank" rel="noopener" href="https://cmake.org/download/">https://cmake.org/download/</a></p>
<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/20210823210956.png" style="zoom:67%;">

<h3 id="1-2-ninja"><a href="#1-2-ninja" class="headerlink" title="1.2 ninja"></a>1.2 ninja</h3><p><a target="_blank" rel="noopener" href="https://github.com/ninja-build/ninja/releases/tag/v1.10.2">https://github.com/ninja-build/ninja/releases/tag/v1.10.2</a></p>
<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/20210823211526.png" style="zoom:50%;">

<p>​    Ninja 是Google的一名程序员推出的注重速度的构建工具，一般在Unix/Linux上的程序通过make/makefile来构建编译，而Ninja通过将编译任务并行组织，提高了构建速度。可以通过cmake来生成ninja的配置，进而进行编译。</p>
<p><img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/20210823211635.png"></p>
<p>​      把它安装到cmake的安装路径：</p>
<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/20210823212036.png" style="zoom:50%;">

<h3 id="1-3-编译工具mingw"><a href="#1-3-编译工具mingw" class="headerlink" title="1.3 编译工具mingw"></a>1.3 编译工具mingw</h3><p>​    把下载下来的x86_64-8.1.0-release-posix-seh-rt_v6-rev0.7z文件解压之后会得到一个mingw64的文件夹，将其拷贝到你要安装的目录。解压完毕之后需要配置环境变量path下添加你的mingw64/bin，如：D:\Pro_Software\mingw64\bin。在cmd窗口使用 gcc -v去查看下。</p>
<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/20210823215506.png" style="zoom:50%;">

<p><strong>mingw-w64 版本选择方法</strong>   </p>
<p>​    32位系统选择i686, 64位系统选择x86_64   </p>
<p>​    线程模型：win32 : 没有C ++ 11多线程特性, posix : 支持C ++ 11多线程特性   </p>
<p>​    异常处理模型:32位系统推荐dwarf，64位系统推荐seh   </p>
<p>​    因为测试环境为64位系统，所以推荐配置为x86_64-posix-seh</p>
<h3 id="1-4-vscode插件安装"><a href="#1-4-vscode插件安装" class="headerlink" title="1.4 vscode插件安装"></a>1.4 vscode插件安装</h3><p>​    我安装了下面这些插件：</p>
<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/20210823221753.png" style="zoom:50%;">

<h2 id="2-实例演示"><a href="#2-实例演示" class="headerlink" title="2.实例演示"></a>2.实例演示</h2><h3 id="2-1-只有main-c文件的工程"><a href="#2-1-只有main-c文件的工程" class="headerlink" title="2.1 只有main.c文件的工程"></a>2.1 只有main.c文件的工程</h3><p>​    我们很多时候，只是为了在vscode上写一小段C代码来验证功能，这个时候其实根本不需要用makefile或者cmake来构建工程，下面就简单演示一下。</p>
<p>​    (1)main.c文件写好之后保存文件，然后点击菜单栏的运行，再点击启动调试</p>
<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/微信截图_20210824094955.png" style="zoom:50%;">

<p>​    (2)编译环境选择“GDB/LLDB”，Windows那个是给MSVC编译器用的，MingGW需要使用GDB。</p>
<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/微信截图_20210824095404.png" style="zoom:70%;">

<p>​    (3)配置选择gcc.exe(如果是C++就选择g++编译器!)</p>
<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/微信截图_20210824095527.png" style="zoom:67%;">

<p>​    (4)如果是第一次，这个动作会产生以下几个效果:自动创建并打开launch.json文件，自动打开调试控制台。同时，代码会被自动编译，并自动以调试模式执行一遍生成的程序。如果代码没什么问题，且未设置任何断点或等待外部输入的代码，这个黄框一般只会闪现一下，程序一结束就会回归正常。</p>
<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/微信截图_20210824100655.png" style="zoom:50%;">

<p>​    <strong>这一串动作的主要目的是让VS Code自动创建好tasks.json和launch.json文件</strong>，只要这两个文件内容正确，以后的调试工作就可以直接进行而无需再额外配置。</p>
<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/微信截图_20210824101047.png" style="zoom:59%;">

<p>​    (5)具体看一下launch.json和tasks.json文件</p>
<p><img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/%E5%BE%AE%E4%BF%A1%E6%88%AA%E5%9B%BE_20210824102813.png"></p>
<p>​    “name”: “gcc.exe - 生成和调试活动文件”,      // 该调试任务的名字，启动调试时会在待选列表中显示。</p>
<p>​    “stopAtEntry”: false,  // 这一项控制是否在入口处暂停，默认false不暂停，改为true暂停。</p>
<p>​    “externalConsole”: false,  // 这一项控制是否启动外部控制台（单独的黑框）运行程序，默认false表示在集成终端中运行。</p>
<p>​    “preLaunchTask”: “C/C++: gcc.exe 生成活动文件”   <strong>// 调试前的预执行任务，这里的值是tasks.json文件中对应的编译任务，也就是调试前需要先编译</strong>。</p>
<p><img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/%E5%BE%AE%E4%BF%A1%E6%88%AA%E5%9B%BE_20210824103927.png"></p>
<p>​    (6)tasks.json和launch.json文件创建好后，在这个打开的文件夹下所有“单文件的代码”就都可以直接顺利的调试了。</p>
<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/微信截图_20210824104500.png" style="zoom:50%;">

<h3 id="2-2-cmake管理复杂的项目"><a href="#2-2-cmake管理复杂的项目" class="headerlink" title="2.2 cmake管理复杂的项目"></a>2.2 cmake管理复杂的项目</h3><p>​    项目工程长下面这个样子：</p>
<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/微信截图_20210824192713.png" style="zoom:65%;">

<p>​    (1)按ctrl+shift+p：</p>
<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/微信截图_20210824193001.png" style="zoom:70%;">

<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/2微信截图_20210824193104.png" style="zoom:67%;">

<p>​    选择上面这个编译器，用它来构建环境。</p>
<p>​    注意在CMakeLists.txt文件的第13行，这个在windows上叫libm.a，在ubuntu上交libm.so。可以在你MinGW安装目录的lib目录下找到这个libm.a文件，这是一个数学库。</p>
<p>​    (2)然后配置项目：</p>
<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/微信截图_20210824200537.png" style="zoom:67%;">

<p>​    (3)生成可执行文件</p>
<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/微信截图_20210824200746.png" style="zoom:60%;">

<p>​    到这里可执行文件就生成了，后面就可以运行那个可执行文件了。</p>
<p>​    (4)用gdb调试程序</p>
<p>​    Cmakelists.txt中需要添加：set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} -g)是必须的，否则无法正常调试。</p>
<p>​    选择“运行”-&gt;”添加配置”，<strong>后面记得选择“默认配置”，这样就只生成一个launch.json文件，否则还会生成tasks.json，因为我们这里用cmake已经编译出了可执行程序，所以不需要tasks.json文件。</strong></p>
<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/微信截图_20210824201916.png" style="zoom:67%;">

<p>​    将生成的默认launch.json文件修改成如下所示：</p>
<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/微信截图_20210824202516.png" style="zoom:67%;">

<p>​    这样就可以用gdb调试了：</p>
<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/微信截图_20210824202759.png" style="zoom:67%;">
      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/08/12/stm32%20usb%20cdc%E6%97%A0%E6%B3%95%E5%8F%91%E9%80%8164%E6%95%B4%E6%95%B0%E5%80%8D%E6%95%B0%E6%8D%AE/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="xudonglei">
      <meta itemprop="description" content="搬砖 嵌入式">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="学无止境">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/2021/08/12/stm32%20usb%20cdc%E6%97%A0%E6%B3%95%E5%8F%91%E9%80%8164%E6%95%B4%E6%95%B0%E5%80%8D%E6%95%B0%E6%8D%AE/" class="post-title-link" itemprop="url">stm32 usb cdc无法发送64整数倍数据</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-08-12 18:32:40" itemprop="dateCreated datePublished" datetime="2021-08-12T18:32:40+08:00">2021-08-12</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F/" itemprop="url" rel="index"><span itemprop="name">嵌入式</span></a>
                </span>
            </span>

          
            <span id="/2021/08/12/stm32%20usb%20cdc%E6%97%A0%E6%B3%95%E5%8F%91%E9%80%8164%E6%95%B4%E6%95%B0%E5%80%8D%E6%95%B0%E6%8D%AE/" class="post-meta-item leancloud_visitors" data-flag-title="stm32 usb cdc无法发送64整数倍数据" title="阅读次数">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span class="leancloud-visitors-count"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="fa fa-comment-o"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2021/08/12/stm32%20usb%20cdc%E6%97%A0%E6%B3%95%E5%8F%91%E9%80%8164%E6%95%B4%E6%95%B0%E5%80%8D%E6%95%B0%E6%8D%AE/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2021/08/12/stm32%20usb%20cdc%E6%97%A0%E6%B3%95%E5%8F%91%E9%80%8164%E6%95%B4%E6%95%B0%E5%80%8D%E6%95%B0%E6%8D%AE/" itemprop="commentCount"></span>
    </a>
  </span>
  
  

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <p><strong>问题：</strong></p>
<p>​        之前就看到网友有反馈说做stm32 usb cdc开发的时候，无法发送64整数倍的数据。我也试了一下，发现没有问题，可以发送的，因为比较新的cubemx生成的固件中已经修复了这个问题。</p>
<p><strong>原因：</strong></p>
<p>​        当发送数据长度恰好是Data In端点的最大包长整数倍时，最后一包数据必须是零长度的数据包(ZLP)。这是由于在USB标准中，接收端并不是通过已经接收的数据长度来判断是否接收完成，且发送端也并没有给出将要发送多长的数据，因此，接收端在接收数据前，并不知道将要接收的数据是多少，接收端是通过下面两点来判断当前数据已经全部接收了。</p>
<ul>
<li>若接收到的数据包长不足最大包长时，则认为当前传输完成。</li>
<li>如接收到的数据包长为零时，则认为当前传输完成。</li>
</ul>
<p>​        当传输的数据刚好是端点的最大包长时，当发送完最后一包(比如64个字节)时，接收端无法判断是否传输结束，进而继续等待下一包数据。这个就是问题本质所在。</p>
<p><strong>解决方法：</strong></p>
<p>​        在发送完最后一包数据后，判断发送的包长是否为端点最大包长的整数倍，如是，则补发一个零长度的数据包(ZLP)。stm32官方固件中已经做了相应的处理，如下图红框所示：</p>
<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/微信截图_20210812172121.png" style="zoom:80%;">
      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/08/12/stm32%E4%B8%8B%E8%BD%BD%E7%A8%8B%E5%BA%8F%E5%90%8E%E4%B8%8D%E8%AF%86%E5%88%ABusb%E5%8F%A3/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="xudonglei">
      <meta itemprop="description" content="搬砖 嵌入式">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="学无止境">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/2021/08/12/stm32%E4%B8%8B%E8%BD%BD%E7%A8%8B%E5%BA%8F%E5%90%8E%E4%B8%8D%E8%AF%86%E5%88%ABusb%E5%8F%A3/" class="post-title-link" itemprop="url">stm32下载程序后不识别usb口</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-08-12 16:44:07" itemprop="dateCreated datePublished" datetime="2021-08-12T16:44:07+08:00">2021-08-12</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F/" itemprop="url" rel="index"><span itemprop="name">嵌入式</span></a>
                </span>
            </span>

          
            <span id="/2021/08/12/stm32%E4%B8%8B%E8%BD%BD%E7%A8%8B%E5%BA%8F%E5%90%8E%E4%B8%8D%E8%AF%86%E5%88%ABusb%E5%8F%A3/" class="post-meta-item leancloud_visitors" data-flag-title="stm32下载程序后不识别usb口" title="阅读次数">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span class="leancloud-visitors-count"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="fa fa-comment-o"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2021/08/12/stm32%E4%B8%8B%E8%BD%BD%E7%A8%8B%E5%BA%8F%E5%90%8E%E4%B8%8D%E8%AF%86%E5%88%ABusb%E5%8F%A3/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2021/08/12/stm32%E4%B8%8B%E8%BD%BD%E7%A8%8B%E5%BA%8F%E5%90%8E%E4%B8%8D%E8%AF%86%E5%88%ABusb%E5%8F%A3/" itemprop="commentCount"></span>
    </a>
  </span>
  
  

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <p><strong>问题：</strong></p>
<p>​    stm32的usb例程每次下载完程序之后都需要重新插拔一次usb口才可以识别串口。</p>
<p><strong>原因：</strong></p>
<p>​    由于芯片在下载完程序后没有重新枚举所导致的。需要在设备上电后对USB进行重新枚举即可，使用方法为将USB DP（PA12）引脚拉低一段时间后即可。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*USB 重新枚举函数*/</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">USB_Reset</span><span class="params">(<span class="keyword">void</span>)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">  	GPIO_InitTypeDef GPIO_InitStruct = &#123;<span class="number">0</span>&#125;;</span><br><span class="line">	__HAL_RCC_GPIOA_CLK_ENABLE();</span><br><span class="line">	GPIO_InitStruct.Pin = GPIO_PIN_12;</span><br><span class="line">	GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;</span><br><span class="line">	GPIO_InitStruct.Pull = GPIO_NOPULL;</span><br><span class="line">	GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;</span><br><span class="line">	HAL_GPIO_Init(GPIOA, &amp;GPIO_InitStruct);</span><br><span class="line">	HAL_GPIO_WritePin(GPIOA,GPIO_PIN_12,GPIO_PIN_RESET);</span><br><span class="line">	HAL_Delay(<span class="number">100</span>);</span><br><span class="line">	HAL_GPIO_WritePin(GPIOA,GPIO_PIN_12,GPIO_PIN_SET);</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>将这个函数放在main函数靠前的地方，USB初始化的前面即可。</p>
<p><strong>补充知识点：</strong>(摘自于网络：<a target="_blank" rel="noopener" href="https://www.cnblogs.com/ancongliang/p/10270832.html">https://www.cnblogs.com/ancongliang/p/10270832.html</a>)</p>
<p>USB可以热插拔的硬件原理</p>
<p>​    在USB集线器(hub)的每个下游端口的D+和D-上，分别接了一个15K欧姆的下拉电阻到地。这样，在集线器的端口悬空时，就被这两个下拉电阻拉到了低电平。</p>
<p>​    而在USB设备端，在D+或者D-上接了1.5K欧姆上拉电阻。对于全速和高速设备，上拉电阻是接在D+上；而低速设备则是上拉电阻接在D-上。这样，当设备插入到集线器时，由1.5K的上拉电阻和15K的下拉电阻分压，结果就将差分数据线中的一条拉高了。集线器检测到这个状态后，它就报告给USB主控制器（或者通过它上一层的集线器报告给USB主控制器），这样就检测到设备的插入了。USB高速设备先是被识别为全速设备，然后通过HOST和DEVICE两者之间的确认，再切换到高速模式的。在高速模式下，是电流传输模式，这时将D+上的上拉电阻断开。</p>

      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/08/10/USB3341%E7%9A%84PIN20%E5%BC%95%E8%84%9A%E5%BC%95%E5%8F%91%E7%9A%84%E9%97%AE%E9%A2%98/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="xudonglei">
      <meta itemprop="description" content="搬砖 嵌入式">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="学无止境">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/2021/08/10/USB3341%E7%9A%84PIN20%E5%BC%95%E8%84%9A%E5%BC%95%E5%8F%91%E7%9A%84%E9%97%AE%E9%A2%98/" class="post-title-link" itemprop="url">USB3341的PIN20引脚引发的问题</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-08-10 13:58:48" itemprop="dateCreated datePublished" datetime="2021-08-10T13:58:48+08:00">2021-08-10</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E7%A1%AC%E4%BB%B6/" itemprop="url" rel="index"><span itemprop="name">硬件</span></a>
                </span>
            </span>

          
            <span id="/2021/08/10/USB3341%E7%9A%84PIN20%E5%BC%95%E8%84%9A%E5%BC%95%E5%8F%91%E7%9A%84%E9%97%AE%E9%A2%98/" class="post-meta-item leancloud_visitors" data-flag-title="USB3341的PIN20引脚引发的问题" title="阅读次数">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span class="leancloud-visitors-count"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="fa fa-comment-o"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2021/08/10/USB3341%E7%9A%84PIN20%E5%BC%95%E8%84%9A%E5%BC%95%E5%8F%91%E7%9A%84%E9%97%AE%E9%A2%98/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2021/08/10/USB3341%E7%9A%84PIN20%E5%BC%95%E8%84%9A%E5%BC%95%E5%8F%91%E7%9A%84%E9%97%AE%E9%A2%98/" itemprop="commentCount"></span>
    </a>
  </span>
  
  

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <p>​    USB3341电路接法采用ULPI Clock Out Mode，在这种模式下，PIN20引脚REFCLK由mcu输入一定频率的时钟，PIN1引脚CLKOUT就可以输出60MHz的ULPI Clock。</p>
<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/微信截图_20210810114050.png" style="zoom:80%;">

<p>​    出问题主要在PIN20引脚输入的时钟频率不知道，找了datesheet上好像也没写清楚，一开始错误的给了60MHz的时钟，后来无意中看到了USB3341另一种版本的数据手册，上面就写的清清楚楚，明明白白。</p>
<p><img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20210810135153.jpg"></p>
<p>​    右边那个文档里面就写清楚了，左边那个文档就没写，狗日的：</p>
<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/微信截图_20210810135410.png" style="zoom:80%;">

<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/微信截图_20210810135548.png" style="zoom:67%;">
      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/08/06/STM32%20MCO%E6%97%B6%E9%92%9F%E8%BE%93%E5%87%BA/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="xudonglei">
      <meta itemprop="description" content="搬砖 嵌入式">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="学无止境">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/2021/08/06/STM32%20MCO%E6%97%B6%E9%92%9F%E8%BE%93%E5%87%BA/" class="post-title-link" itemprop="url">STM32 MCO时钟输出</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-08-06 11:03:55" itemprop="dateCreated datePublished" datetime="2021-08-06T11:03:55+08:00">2021-08-06</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F/" itemprop="url" rel="index"><span itemprop="name">嵌入式</span></a>
                </span>
            </span>

          
            <span id="/2021/08/06/STM32%20MCO%E6%97%B6%E9%92%9F%E8%BE%93%E5%87%BA/" class="post-meta-item leancloud_visitors" data-flag-title="STM32 MCO时钟输出" title="阅读次数">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span class="leancloud-visitors-count"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="fa fa-comment-o"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2021/08/06/STM32%20MCO%E6%97%B6%E9%92%9F%E8%BE%93%E5%87%BA/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2021/08/06/STM32%20MCO%E6%97%B6%E9%92%9F%E8%BE%93%E5%87%BA/" itemprop="commentCount"></span>
    </a>
  </span>
  
  

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <p>​     STM32的PA8引脚具有复用功能——时钟输出(MCO)， 该功能能将STM32内部的时钟通过PA.8输出.</p>
<p>​    使用cubemx来生成代码步骤如下：</p>
<p>1.</p>
<p><img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/mco-1.png"></p>
<p>2.</p>
<p><img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/mco-2.png"></p>
<p>3.</p>
<p><img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/mco-3.png"></p>
<p>用示波器看那个引脚输出的波形，如下所示：</p>
<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/GPIO_SPEED_FREQ_VERY_HIGH.jpg" style="zoom:37%;">

<p><strong>注意：GPIO输出速度</strong></p>
<p>​    用STM32CubeMX配置GPIO输出引脚，都会有速度*<strong>*GPIO_InitStruct.Speed**</strong>这个选项，速度的配置，就是决定IO口驱动电路的响应速度。如果速度配置太低，就无法输出高的时钟。</p>
<p><strong><em>\</em>高低速差异：**</strong></p>
<p><strong><em>\</em>配置高速：**</strong>输出频率高，噪音大，功耗高，电磁干扰强； </p>
<p><strong><em>*</em>配置低速：*</strong>输出频率低，噪音小，功耗低，电磁干扰弱；提高系统EMI（电磁干扰）性能。</p>

      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/08/05/%E9%80%9A%E8%BF%87map%E6%96%87%E4%BB%B6%E5%AE%9A%E4%BD%8DHardFault%E9%97%AE%E9%A2%98/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="xudonglei">
      <meta itemprop="description" content="搬砖 嵌入式">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="学无止境">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/2021/08/05/%E9%80%9A%E8%BF%87map%E6%96%87%E4%BB%B6%E5%AE%9A%E4%BD%8DHardFault%E9%97%AE%E9%A2%98/" class="post-title-link" itemprop="url">通过map文件定位HardFault问题</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-08-05 10:49:39" itemprop="dateCreated datePublished" datetime="2021-08-05T10:49:39+08:00">2021-08-05</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F/" itemprop="url" rel="index"><span itemprop="name">嵌入式</span></a>
                </span>
            </span>

          
            <span id="/2021/08/05/%E9%80%9A%E8%BF%87map%E6%96%87%E4%BB%B6%E5%AE%9A%E4%BD%8DHardFault%E9%97%AE%E9%A2%98/" class="post-meta-item leancloud_visitors" data-flag-title="通过map文件定位HardFault问题" title="阅读次数">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span class="leancloud-visitors-count"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="fa fa-comment-o"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2021/08/05/%E9%80%9A%E8%BF%87map%E6%96%87%E4%BB%B6%E5%AE%9A%E4%BD%8DHardFault%E9%97%AE%E9%A2%98/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2021/08/05/%E9%80%9A%E8%BF%87map%E6%96%87%E4%BB%B6%E5%AE%9A%E4%BD%8DHardFault%E9%97%AE%E9%A2%98/" itemprop="commentCount"></span>
    </a>
  </span>
  
  

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <p><strong>问题：</strong></p>
<p><img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/%E5%BE%AE%E4%BF%A1%E6%88%AA%E5%9B%BE_20210805103357.png"></p>
<p>​    调试发现程序每次执行完358行，就立马进入hardfault，仔细排查了数组内存访问有无yue jie，有无cai内存，查了很久，也没发现什么问题。</p>
<p><strong>解决：</strong></p>
<p>​    把354行和358行合起来分析，358行涉及到内存的读取，难道是fei fa读取了内存，这是可以怀疑的一个点。直接看map文件：</p>
<p><img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/%E5%BE%AE%E4%BF%A1%E6%88%AA%E5%9B%BE_20210805104220.png"></p>
<p>FrameData在内存中被分配的地址位于0x20000226，没有8字节对齐访问。</p>
<p>​    将FrameData初始化的时候加个对齐：</p>
<p><img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/%E5%BE%AE%E4%BF%A1%E6%88%AA%E5%9B%BE_20210805104718.png"></p>
<p>​    这时候生成的map文件如下：</p>
<p><img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/%E5%BE%AE%E4%BF%A1%E6%88%AA%E5%9B%BE_20210805104903.png"></p>
<p>再运行问题解决。</p>

      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  

      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/08/04/HAL_UART_Transmit_DMA%E5%8F%AA%E8%83%BD%E5%8F%91%E9%80%81%E4%B8%80%E6%AC%A1%E6%95%B0%E6%8D%AE%E9%97%AE%E9%A2%98/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="xudonglei">
      <meta itemprop="description" content="搬砖 嵌入式">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="学无止境">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          
            <a href="/2021/08/04/HAL_UART_Transmit_DMA%E5%8F%AA%E8%83%BD%E5%8F%91%E9%80%81%E4%B8%80%E6%AC%A1%E6%95%B0%E6%8D%AE%E9%97%AE%E9%A2%98/" class="post-title-link" itemprop="url">HAL_UART_Transmit_DMA只能发送一次数据问题</a>
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-08-04 19:39:22" itemprop="dateCreated datePublished" datetime="2021-08-04T19:39:22+08:00">2021-08-04</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F/" itemprop="url" rel="index"><span itemprop="name">嵌入式</span></a>
                </span>
            </span>

          
            <span id="/2021/08/04/HAL_UART_Transmit_DMA%E5%8F%AA%E8%83%BD%E5%8F%91%E9%80%81%E4%B8%80%E6%AC%A1%E6%95%B0%E6%8D%AE%E9%97%AE%E9%A2%98/" class="post-meta-item leancloud_visitors" data-flag-title="HAL_UART_Transmit_DMA只能发送一次数据问题" title="阅读次数">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span class="leancloud-visitors-count"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="fa fa-comment-o"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2021/08/04/HAL_UART_Transmit_DMA%E5%8F%AA%E8%83%BD%E5%8F%91%E9%80%81%E4%B8%80%E6%AC%A1%E6%95%B0%E6%8D%AE%E9%97%AE%E9%A2%98/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2021/08/04/HAL_UART_Transmit_DMA%E5%8F%AA%E8%83%BD%E5%8F%91%E9%80%81%E4%B8%80%E6%AC%A1%E6%95%B0%E6%8D%AE%E9%97%AE%E9%A2%98/" itemprop="commentCount"></span>
    </a>
  </span>
  
  

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
          <p><strong>问题：</strong></p>
<p>​    HAL_UART_Transmit_DMA函数只能调用一次，第二次就返回状态HAL_UART_STATE_BUSY。</p>
<p><strong>原因：</strong></p>
<p>​    如果不开启串口中断，则程序只能发送一次数据,程序不能判断DMA传输是否完成，USART一直处于busy状态。这个在stm32f4xx_hal_uart.c文件的开头处也有说明。使用DMA非循环模式时，用来检测最后一个字节发送完毕。</p>
<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/微信截图_20210804111656.png" style="zoom:80%;">

<p>实际代码中写的如下所示：</p>
<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/微信截图_20210804112345.png" style="zoom:67%;">
      
    </div>

    
    
    
      <footer class="post-footer">
        <div class="post-eof"></div>
      </footer>
  </article>
  
  
  


  
  <nav class="pagination">
    <a class="extend prev" rel="prev" href="/page/20/"><i class="fa fa-angle-left" aria-label="上一页"></i></a><a class="page-number" href="/">1</a><span class="space">&hellip;</span><a class="page-number" href="/page/20/">20</a><span class="page-number current">21</span><a class="page-number" href="/page/22/">22</a><span class="space">&hellip;</span><a class="page-number" href="/page/29/">29</a><a class="extend next" rel="next" href="/page/22/"><i class="fa fa-angle-right" aria-label="下一页"></i></a>
  </nav>



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">xudonglei</p>
  <div class="site-description" itemprop="description">搬砖 嵌入式</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">283</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">19</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">67</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2024</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">xudonglei</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://muse.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/muse.js"></script>


<script src="/js/next-boot.js"></script>




  




  
<script src="/js/local-search.js"></script>













  

  


<script>
NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
  NexT.utils.getScript('//unpkg.com/valine/dist/Valine.min.js', () => {
    var GUEST = ['nick', 'mail', 'link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item => {
      return GUEST.includes(item);
    });
    new Valine({
      el         : '#valine-comments',
      verify     : false,
      notify     : false,
      appId      : 'BT0Qw9HcTpPv1a6cT1iDBXKz-gzGzoHsz',
      appKey     : 'LMCMh8v02JTMFAwKNRcgoizI',
      placeholder: "Just go go",
      avatar     : 'mm',
      meta       : guest,
      pageSize   : '10' || 10,
      visitor    : true,
      lang       : 'zh-CN' || 'zh-cn',
      path       : location.pathname,
      recordIP   : false,
      serverURLs : ''
    });
  }, window.Valine);
});
</script>

</body>
</html>
